## distribution data preparation ####

library(sp)
library(dismo)
## Loading required package: raster
library(raster)
library(gbm)
## Loaded gbm 2.1.5
library(maptools)
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
library(rgdal)
## rgdal: version: 1.4-6, (SVN revision 841)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/ich/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/ich/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.3-1
library(sp)
library(shapefiles)
## Loading required package: foreign
## 
## Attaching package: 'shapefiles'
## The following objects are masked from 'package:foreign':
## 
##     read.dbf, write.dbf
# DATA distribution

vignette('sdm', 'dismo')

distribution_shape <- read.shp("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_Pakistan_all_test.shp")

distribution_dataframe <- as.data.frame(distribution_shape)

write.csv(distribution_shape, file = "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv")

occurrence_csv <- paste0(system.file(package="dismo"), "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv")
head(occurrence_csv)
## [1] "C:/Users/ich/Documents/R/win-library/3.6/dismoC:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv"
occurrence <- read.csv("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv")
head(occurrence)
##   X shp.record    shp.x    shp.y shp.shape.type header.file.code
## 1 1          1 72.07099 35.99492              1             9994
## 2 2          2 71.99009 35.15735              1             9994
## 3 3          3 73.39397 34.45065              1             9994
## 4 4          4 73.16466 33.82338              1             9994
## 5 5          5 73.16823 34.13390              1             9994
## 6 6          6 73.41688 34.00422              1             9994
##   header.file.length header.file.version header.shape.type header.xmin
## 1              82230                1000                 1    62.23056
## 2              82230                1000                 1    62.23056
## 3              82230                1000                 1    62.23056
## 4              82230                1000                 1    62.23056
## 5              82230                1000                 1    62.23056
## 6              82230                1000                 1    62.23056
##   header.ymin header.xmax header.ymax header.zmin header.zmax header.mmin
## 1    24.13072    77.77783    37.11705           0           0           0
## 2    24.13072    77.77783    37.11705           0           0           0
## 3    24.13072    77.77783    37.11705           0           0           0
## 4    24.13072    77.77783    37.11705           0           0           0
## 5    24.13072    77.77783    37.11705           0           0           0
## 6    24.13072    77.77783    37.11705           0           0           0
##   header.mmax
## 1           0
## 2           0
## 3           0
## 4           0
## 5           0
## 6           0
occurrence_lonlat <- occurrence [,3:4]
head(occurrence_lonlat)
##      shp.x    shp.y
## 1 72.07099 35.99492
## 2 71.99009 35.15735
## 3 73.39397 34.45065
## 4 73.16466 33.82338
## 5 73.16823 34.13390
## 6 73.41688 34.00422
# dataframe distribution data

dataframe_occurrence_record_lonlat <- occurrence [,2:4]
head(dataframe_occurrence_record_lonlat)
##   shp.record    shp.x    shp.y
## 1          1 72.07099 35.99492
## 2          2 71.99009 35.15735
## 3          3 73.39397 34.45065
## 4          4 73.16466 33.82338
## 5          5 73.16823 34.13390
## 6          6 73.41688 34.00422
write.csv(dataframe_occurrence_record_lonlat, "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/dataframe_occurrence_record_lonlat.csv")
head(dataframe_occurrence_record_lonlat)
##   shp.record    shp.x    shp.y
## 1          1 72.07099 35.99492
## 2          2 71.99009 35.15735
## 3          3 73.39397 34.45065
## 4          4 73.16466 33.82338
## 5          5 73.16823 34.13390
## 6          6 73.41688 34.00422
## species data preparation ####
# read in species_csv

species_subspecies <- read.csv("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/distribution_Pakistan_all_species.csv")

species_subspecies_cut <- species_subspecies [,0:2]
head(species_subspecies_cut)
##           species.C.80 subspecies.C.80
## 1      Actinor_radians                
## 2      Actinor_radians                
## 3      Actinor_radians                
## 4 Aeromachus_stigmatus                
## 5 Aeromachus_stigmatus                
## 6 Aeromachus_stigmatus
## combine species + distribution + cut (subset) for only 1 species (--> Gegenes_nostrodamus) ####

species_distribution_combined <- data.frame(dataframe_occurrence_record_lonlat, species_subspecies_cut)
head(species_distribution_combined)
##   shp.record    shp.x    shp.y         species.C.80 subspecies.C.80
## 1          1 72.07099 35.99492      Actinor_radians                
## 2          2 71.99009 35.15735      Actinor_radians                
## 3          3 73.39397 34.45065      Actinor_radians                
## 4          4 73.16466 33.82338 Aeromachus_stigmatus                
## 5          5 73.16823 34.13390 Aeromachus_stigmatus                
## 6          6 73.41688 34.00422 Aeromachus_stigmatus
# subset for species: Gegenes_nostrodamus

subset_Gegenes_nostrodamus <- subset(species_distribution_combined, species.C.80=="Gegenes_nostrodamus")
subset_Gegenes_nostrodamus
##    shp.record    shp.x    shp.y        species.C.80 subspecies.C.80
## 53         53 74.75619 35.27489 Gegenes_nostrodamus     nostrodamus
## 54         54 74.25131 35.28893 Gegenes_nostrodamus     nostrodamus
## 55         55 72.38800 35.94713 Gegenes_nostrodamus     nostrodamus
## 56         56 71.90671 35.83228 Gegenes_nostrodamus     nostrodamus
## 57         57 72.02838 35.21013 Gegenes_nostrodamus     nostrodamus
## 58         58 71.88014 34.80043 Gegenes_nostrodamus     nostrodamus
## 59         59 71.90288 34.46425 Gegenes_nostrodamus     nostrodamus
## 60         60 73.43660 33.89194 Gegenes_nostrodamus     nostrodamus
## 61         61 73.09218 34.16677 Gegenes_nostrodamus     nostrodamus
## 62         62 72.90301 33.97403 Gegenes_nostrodamus     nostrodamus
## 63         63 73.09396 33.89551 Gegenes_nostrodamus     nostrodamus
## 64         64 72.61379 33.70444 Gegenes_nostrodamus     nostrodamus
## 65         65 72.81188 33.69195 Gegenes_nostrodamus     nostrodamus
## 66         66 74.17204 31.62093 Gegenes_nostrodamus     nostrodamus
## 67         67 73.99001 31.47994 Gegenes_nostrodamus     nostrodamus
## 68         68 66.75859 30.43480 Gegenes_nostrodamus     nostrodamus
## 69         69 66.72290 30.06361 Gegenes_nostrodamus     nostrodamus
## 70         70 67.00843 24.94896 Gegenes_nostrodamus     nostrodamus
## 71         71 73.06086 33.49902 Gegenes_nostrodamus         karsana
subset_Gegenes_nostrodamus_lat_lon <- subset_Gegenes_nostrodamus[,2:3]
subset_Gegenes_nostrodamus_lat_lon
##       shp.x    shp.y
## 53 74.75619 35.27489
## 54 74.25131 35.28893
## 55 72.38800 35.94713
## 56 71.90671 35.83228
## 57 72.02838 35.21013
## 58 71.88014 34.80043
## 59 71.90288 34.46425
## 60 73.43660 33.89194
## 61 73.09218 34.16677
## 62 72.90301 33.97403
## 63 73.09396 33.89551
## 64 72.61379 33.70444
## 65 72.81188 33.69195
## 66 74.17204 31.62093
## 67 73.99001 31.47994
## 68 66.75859 30.43480
## 69 66.72290 30.06361
## 70 67.00843 24.94896
## 71 73.06086 33.49902
#final presence data

p_a <- 1

final_presence <- cbind(subset_Gegenes_nostrodamus_lat_lon, p_a)
final_presence
##       shp.x    shp.y p_a
## 53 74.75619 35.27489   1
## 54 74.25131 35.28893   1
## 55 72.38800 35.94713   1
## 56 71.90671 35.83228   1
## 57 72.02838 35.21013   1
## 58 71.88014 34.80043   1
## 59 71.90288 34.46425   1
## 60 73.43660 33.89194   1
## 61 73.09218 34.16677   1
## 62 72.90301 33.97403   1
## 63 73.09396 33.89551   1
## 64 72.61379 33.70444   1
## 65 72.81188 33.69195   1
## 66 74.17204 31.62093   1
## 67 73.99001 31.47994   1
## 68 66.75859 30.43480   1
## 69 66.72290 30.06361   1
## 70 67.00843 24.94896   1
## 71 73.06086 33.49902   1
## chelsa data preparation ####

#merge

merged_chelsa_annual_mean_temp_path <- "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/"
merged_chelsa_annual_mean_temp_path
## [1] "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/"
merged_chelsa_annual_mean_temp_files <- list.files("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/")
merged_chelsa_annual_mean_temp_files
##  [1] "CHELSA_temp10_01_1979-2013_V1.2_land.tif"    
##  [2] "CHELSA_temp10_02_1979-2013_V1.2_land.tif"    
##  [3] "CHELSA_temp10_03_1979-2013_V1.2_land.tif"    
##  [4] "CHELSA_temp10_04_1979-2013_V1.2_land.tif"    
##  [5] "CHELSA_temp10_05_1979-2013_V1.2_land.tif"    
##  [6] "CHELSA_temp10_06_1979-2013_V1.2_land.tif"    
##  [7] "CHELSA_temp10_07_1979-2013_V1.2_land.tif"    
##  [8] "CHELSA_temp10_08_1979-2013_V1.2_land (1).tif"
##  [9] "CHELSA_temp10_09_1979-2013_V1.2_land.tif"    
## [10] "CHELSA_temp10_10_1979-2013_V1.2_land.tif"    
## [11] "CHELSA_temp10_11_1979-2013_V1.2_land.tif"    
## [12] "CHELSA_temp10_12_1979-2013_V1.2_land.tif"    
## [13] "merged"
raster_list <- list(merged_chelsa_annual_mean_temp_files) 
raster_list
## [[1]]
##  [1] "CHELSA_temp10_01_1979-2013_V1.2_land.tif"    
##  [2] "CHELSA_temp10_02_1979-2013_V1.2_land.tif"    
##  [3] "CHELSA_temp10_03_1979-2013_V1.2_land.tif"    
##  [4] "CHELSA_temp10_04_1979-2013_V1.2_land.tif"    
##  [5] "CHELSA_temp10_05_1979-2013_V1.2_land.tif"    
##  [6] "CHELSA_temp10_06_1979-2013_V1.2_land.tif"    
##  [7] "CHELSA_temp10_07_1979-2013_V1.2_land.tif"    
##  [8] "CHELSA_temp10_08_1979-2013_V1.2_land (1).tif"
##  [9] "CHELSA_temp10_09_1979-2013_V1.2_land.tif"    
## [10] "CHELSA_temp10_10_1979-2013_V1.2_land.tif"    
## [11] "CHELSA_temp10_11_1979-2013_V1.2_land.tif"    
## [12] "CHELSA_temp10_12_1979-2013_V1.2_land.tif"    
## [13] "merged"
for(i in 1:12)
{
  tilesloop <- paste0(merged_chelsa_annual_mean_temp_path, merged_chelsa_annual_mean_temp_files[i])
  tilesloop       
  raster_list[[i]] <- raster(tilesloop)
  rasterliste <- raster_list[[i]]
  rasterliste
}

raster_list$filename     <- "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/merged/merged_chelsa_annual_mean_temp.tif"
exporttif <- raster_list$filename 
exporttif
## [1] "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/merged/merged_chelsa_annual_mean_temp.tif"
raster_list$overwrite    <- TRUE


all_merged_chelsa_annual_mean_temp_raster <- do.call(raster::merge, raster_list, quote=TRUE)
all_merged_chelsa_annual_mean_temp_raster
## class      : RasterLayer 
## dimensions : 20880, 43200, 902016000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : -180.0001, 179.9999, -90.00014, 83.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/merged/merged_chelsa_annual_mean_temp.tif 
## names      : merged_chelsa_annual_mean_temp 
## values     : -457, 345  (min, max)
plot(all_merged_chelsa_annual_mean_temp_raster, main="all_merged chelsa annual mean temperature")

chelsa_annual_global_prec <- 
  raster("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_global_prec/CHELSA_bio10_12.tif")
chelsa_prec_warmest_quarter <- 
  raster("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/prec_warmest_quarter/CHELSA_bio10_18.tif")
chelsa_warmest_quarter_mean_temp <- 
  raster("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/warmest_quarter_mean_t_global/CHELSA_bio10_10.tif")

plot(all_merged_chelsa_annual_mean_temp_raster, main="all_merged chelsa annual mean temperature")

plot(chelsa_annual_global_prec, main="chelsa annual global prec")

plot(chelsa_prec_warmest_quarter, main="chelsa prec warmest quarter")

plot(chelsa_warmest_quarter_mean_temp, main="chelsa warmest quarter mean temperature")

# crop onto pakistan extent

extend_pakistan <- extent(c(58, 83, 23, 38))

cropped_chelsa_annual_mean_temp_raster <- crop(all_merged_chelsa_annual_mean_temp_raster, extend_pakistan)
cropped_chelsa_annual_mean_temp_raster
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/RtmporScUs/raster/r_tmp_2020-01-19_003809_11500_76501.grd 
## names      : merged_chelsa_annual_mean_temp 
## values     : -341, 224  (min, max)
plot(cropped_chelsa_annual_mean_temp_raster)

cropped_chelsa_annual_global_prec <- crop(chelsa_annual_global_prec,extend_pakistan)
cropped_chelsa_annual_global_prec
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/RtmporScUs/raster/r_tmp_2020-01-19_003812_11500_29709.grd 
## names      : CHELSA_bio10_12 
## values     : 14, 3406  (min, max)
plot(cropped_chelsa_annual_global_prec)

cropped_chelsa_prec_warmest_quarter <- crop(chelsa_prec_warmest_quarter, extend_pakistan)
cropped_chelsa_prec_warmest_quarter
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/RtmporScUs/raster/r_tmp_2020-01-19_003817_11500_88198.grd 
## names      : CHELSA_bio10_18 
## values     : 0, 3221  (min, max)
plot(cropped_chelsa_prec_warmest_quarter)

cropped_chelsa_warmest_quarter_mean_temp <- crop(chelsa_warmest_quarter_mean_temp, extend_pakistan,)
cropped_chelsa_warmest_quarter_mean_temp
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/RtmporScUs/raster/r_tmp_2020-01-19_003821_11500_81968.grd 
## names      : CHELSA_bio10_10 
## values     : -110, 402  (min, max)
plot(cropped_chelsa_warmest_quarter_mean_temp)

# rasterstack

rasterstack_pakistan_chelsa <- raster::stack(cropped_chelsa_annual_global_prec, 
                                             cropped_chelsa_annual_mean_temp_raster, 
                                             cropped_chelsa_prec_warmest_quarter, 
                                             cropped_chelsa_warmest_quarter_mean_temp)
rasterstack_pakistan_chelsa
## class      : RasterStack 
## dimensions : 1800, 3000, 5400000, 4  (nrow, ncol, ncell, nlayers)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## names      : CHELSA_bio10_12, merged_chelsa_annual_mean_temp, CHELSA_bio10_18, CHELSA_bio10_10 
## min values :              14,                           -341,               0,            -110 
## max values :            3406,                            224,            3221,             402
plot(rasterstack_pakistan_chelsa)

# dataframe chelsa

pakistan_chelsa_dataframe <- as.data.frame(rasterstack_pakistan_chelsa)
head(pakistan_chelsa_dataframe)
##   CHELSA_bio10_12 merged_chelsa_annual_mean_temp CHELSA_bio10_18
## 1             241                             30              22
## 2             229                             32              21
## 3             228                             29              18
## 4             245                             28              18
## 5             281                             22              19
## 6             322                              9              22
##   CHELSA_bio10_10
## 1             286
## 2             288
## 3             285
## 4             283
## 5             277
## 6             263
str(pakistan_chelsa_dataframe)
## 'data.frame':    5400000 obs. of  4 variables:
##  $ CHELSA_bio10_12               : int  241 229 228 245 281 322 351 366 372 373 ...
##  $ merged_chelsa_annual_mean_temp: int  30 32 29 28 22 9 -5 -9 -14 -16 ...
##  $ CHELSA_bio10_18               : int  22 21 18 18 19 22 22 23 23 26 ...
##  $ CHELSA_bio10_10               : int  286 288 285 283 277 263 247 243 237 235 ...
## generate pseudo-absence data ####

pseudo_absence_pakistan <- randomPoints(cropped_chelsa_annual_global_prec, 18)
pseudo_absence_pakistan
##              x        y
##  [1,] 73.73736 26.17903
##  [2,] 76.17069 27.54569
##  [3,] 74.95403 28.76236
##  [4,] 75.26236 28.28736
##  [5,] 68.06236 33.78736
##  [6,] 67.16236 31.47903
##  [7,] 76.53736 27.04569
##  [8,] 78.66236 33.93736
##  [9,] 78.62903 23.36236
## [10,] 82.68736 24.93736
## [11,] 63.35403 31.11236
## [12,] 82.75403 29.58736
## [13,] 60.96236 37.91236
## [14,] 58.13736 26.99569
## [15,] 80.59569 29.72069
## [16,] 66.65403 29.48736
## [17,] 65.74569 28.28736
## [18,] 63.12069 37.47069
head(pseudo_absence_pakistan)
##             x        y
## [1,] 73.73736 26.17903
## [2,] 76.17069 27.54569
## [3,] 74.95403 28.76236
## [4,] 75.26236 28.28736
## [5,] 68.06236 33.78736
## [6,] 67.16236 31.47903
plot(pseudo_absence_pakistan)

# add column "0" for absence:

p_a <- 0

final_absence <- cbind(pseudo_absence_pakistan, p_a)
final_absence
##              x        y p_a
##  [1,] 73.73736 26.17903   0
##  [2,] 76.17069 27.54569   0
##  [3,] 74.95403 28.76236   0
##  [4,] 75.26236 28.28736   0
##  [5,] 68.06236 33.78736   0
##  [6,] 67.16236 31.47903   0
##  [7,] 76.53736 27.04569   0
##  [8,] 78.66236 33.93736   0
##  [9,] 78.62903 23.36236   0
## [10,] 82.68736 24.93736   0
## [11,] 63.35403 31.11236   0
## [12,] 82.75403 29.58736   0
## [13,] 60.96236 37.91236   0
## [14,] 58.13736 26.99569   0
## [15,] 80.59569 29.72069   0
## [16,] 66.65403 29.48736   0
## [17,] 65.74569 28.28736   0
## [18,] 63.12069 37.47069   0
# rename latlon columns

final_absence_df <- as.data.frame(final_absence)
final_absence_df
##           x        y p_a
## 1  73.73736 26.17903   0
## 2  76.17069 27.54569   0
## 3  74.95403 28.76236   0
## 4  75.26236 28.28736   0
## 5  68.06236 33.78736   0
## 6  67.16236 31.47903   0
## 7  76.53736 27.04569   0
## 8  78.66236 33.93736   0
## 9  78.62903 23.36236   0
## 10 82.68736 24.93736   0
## 11 63.35403 31.11236   0
## 12 82.75403 29.58736   0
## 13 60.96236 37.91236   0
## 14 58.13736 26.99569   0
## 15 80.59569 29.72069   0
## 16 66.65403 29.48736   0
## 17 65.74569 28.28736   0
## 18 63.12069 37.47069   0
colnames(final_absence_df)
## [1] "x"   "y"   "p_a"
names(final_absence_df)[1] <- "shp.x"
names(final_absence_df)[2] <- "shp.y"
final_absence_df
##       shp.x    shp.y p_a
## 1  73.73736 26.17903   0
## 2  76.17069 27.54569   0
## 3  74.95403 28.76236   0
## 4  75.26236 28.28736   0
## 5  68.06236 33.78736   0
## 6  67.16236 31.47903   0
## 7  76.53736 27.04569   0
## 8  78.66236 33.93736   0
## 9  78.62903 23.36236   0
## 10 82.68736 24.93736   0
## 11 63.35403 31.11236   0
## 12 82.75403 29.58736   0
## 13 60.96236 37.91236   0
## 14 58.13736 26.99569   0
## 15 80.59569 29.72069   0
## 16 66.65403 29.48736   0
## 17 65.74569 28.28736   0
## 18 63.12069 37.47069   0
## combine presence + absence ####

final_p_a_lat_lon <- rbind(final_presence, final_absence_df)
final_p_a_lat_lon
##       shp.x    shp.y p_a
## 53 74.75619 35.27489   1
## 54 74.25131 35.28893   1
## 55 72.38800 35.94713   1
## 56 71.90671 35.83228   1
## 57 72.02838 35.21013   1
## 58 71.88014 34.80043   1
## 59 71.90288 34.46425   1
## 60 73.43660 33.89194   1
## 61 73.09218 34.16677   1
## 62 72.90301 33.97403   1
## 63 73.09396 33.89551   1
## 64 72.61379 33.70444   1
## 65 72.81188 33.69195   1
## 66 74.17204 31.62093   1
## 67 73.99001 31.47994   1
## 68 66.75859 30.43480   1
## 69 66.72290 30.06361   1
## 70 67.00843 24.94896   1
## 71 73.06086 33.49902   1
## 1  73.73736 26.17903   0
## 2  76.17069 27.54569   0
## 3  74.95403 28.76236   0
## 4  75.26236 28.28736   0
## 5  68.06236 33.78736   0
## 6  67.16236 31.47903   0
## 7  76.53736 27.04569   0
## 8  78.66236 33.93736   0
## 9  78.62903 23.36236   0
## 10 82.68736 24.93736   0
## 11 63.35403 31.11236   0
## 12 82.75403 29.58736   0
## 13 60.96236 37.91236   0
## 14 58.13736 26.99569   0
## 15 80.59569 29.72069   0
## 16 66.65403 29.48736   0
## 17 65.74569 28.28736   0
## 18 63.12069 37.47069   0
## extract rastervalues of chelsa data for whole point data: ####
# only lonlat values for extracting:

final_lat_lon <- final_p_a_lat_lon[,1:2]
final_lat_lon
##       shp.x    shp.y
## 53 74.75619 35.27489
## 54 74.25131 35.28893
## 55 72.38800 35.94713
## 56 71.90671 35.83228
## 57 72.02838 35.21013
## 58 71.88014 34.80043
## 59 71.90288 34.46425
## 60 73.43660 33.89194
## 61 73.09218 34.16677
## 62 72.90301 33.97403
## 63 73.09396 33.89551
## 64 72.61379 33.70444
## 65 72.81188 33.69195
## 66 74.17204 31.62093
## 67 73.99001 31.47994
## 68 66.75859 30.43480
## 69 66.72290 30.06361
## 70 67.00843 24.94896
## 71 73.06086 33.49902
## 1  73.73736 26.17903
## 2  76.17069 27.54569
## 3  74.95403 28.76236
## 4  75.26236 28.28736
## 5  68.06236 33.78736
## 6  67.16236 31.47903
## 7  76.53736 27.04569
## 8  78.66236 33.93736
## 9  78.62903 23.36236
## 10 82.68736 24.93736
## 11 63.35403 31.11236
## 12 82.75403 29.58736
## 13 60.96236 37.91236
## 14 58.13736 26.99569
## 15 80.59569 29.72069
## 16 66.65403 29.48736
## 17 65.74569 28.28736
## 18 63.12069 37.47069
rasValue_pakistan_all <- extract(rasterstack_pakistan_chelsa, final_lat_lon)

rasValue_pakistan_all
##       CHELSA_bio10_12 merged_chelsa_annual_mean_temp CHELSA_bio10_18
##  [1,]             674                           -138              92
##  [2,]             407                           -107              68
##  [3,]             496                           -152              77
##  [4,]             639                           -120              64
##  [5,]            1293                            -10             480
##  [6,]            1320                             37             346
##  [7,]             689                            102             237
##  [8,]            2052                             14             881
##  [9,]            1726                             37             877
## [10,]            1060                            100             461
## [11,]            1677                             45             768
## [12,]             779                            104             102
## [13,]            1060                             96             530
## [14,]             589                            124              91
## [15,]             518                            126              83
## [16,]             212                             61              33
## [17,]             154                             56              35
## [18,]             203                            201              18
## [19,]             916                            105             134
## [20,]             409                            159              75
## [21,]             604                            136              99
## [22,]             326                            146              64
## [23,]             386                            142              79
## [24,]             301                           -101              16
## [25,]             341                             16              29
## [26,]             635                            149              78
## [27,]             106                           -192              71
## [28,]            1352                            165              14
## [29,]            1067                            155             202
## [30,]              87                             89               0
## [31,]             974                           -131             665
## [32,]             133                             33               1
## [33,]             216                            120               5
## [34,]            1957                             58            1363
## [35,]             170                             31              57
## [36,]             152                             93              57
## [37,]             176                             38               1
##       CHELSA_bio10_10
##  [1,]              91
##  [2,]             117
##  [3,]              74
##  [4,]             103
##  [5,]             192
##  [6,]             236
##  [7,]             299
##  [8,]             211
##  [9,]             233
## [10,]             297
## [11,]             243
## [12,]             307
## [13,]             297
## [14,]             330
## [15,]             333
## [16,]             302
## [17,]             292
## [18,]             309
## [19,]             305
## [20,]             337
## [21,]             328
## [22,]             349
## [23,]             341
## [24,]             161
## [25,]             268
## [26,]             339
## [27,]              63
## [28,]             318
## [29,]             320
## [30,]             354
## [31,]              73
## [32,]             313
## [33,]             323
## [34,]             225
## [35,]             256
## [36,]             312
## [37,]             311
## final training data ####

pakistan_butterflies_train_final_Gegenes_nostrodamus <- data.frame(final_p_a_lat_lon, rasValue_pakistan_all)
pakistan_butterflies_train_final_Gegenes_nostrodamus
##       shp.x    shp.y p_a CHELSA_bio10_12 merged_chelsa_annual_mean_temp
## 53 74.75619 35.27489   1             674                           -138
## 54 74.25131 35.28893   1             407                           -107
## 55 72.38800 35.94713   1             496                           -152
## 56 71.90671 35.83228   1             639                           -120
## 57 72.02838 35.21013   1            1293                            -10
## 58 71.88014 34.80043   1            1320                             37
## 59 71.90288 34.46425   1             689                            102
## 60 73.43660 33.89194   1            2052                             14
## 61 73.09218 34.16677   1            1726                             37
## 62 72.90301 33.97403   1            1060                            100
## 63 73.09396 33.89551   1            1677                             45
## 64 72.61379 33.70444   1             779                            104
## 65 72.81188 33.69195   1            1060                             96
## 66 74.17204 31.62093   1             589                            124
## 67 73.99001 31.47994   1             518                            126
## 68 66.75859 30.43480   1             212                             61
## 69 66.72290 30.06361   1             154                             56
## 70 67.00843 24.94896   1             203                            201
## 71 73.06086 33.49902   1             916                            105
## 1  73.73736 26.17903   0             409                            159
## 2  76.17069 27.54569   0             604                            136
## 3  74.95403 28.76236   0             326                            146
## 4  75.26236 28.28736   0             386                            142
## 5  68.06236 33.78736   0             301                           -101
## 6  67.16236 31.47903   0             341                             16
## 7  76.53736 27.04569   0             635                            149
## 8  78.66236 33.93736   0             106                           -192
## 9  78.62903 23.36236   0            1352                            165
## 10 82.68736 24.93736   0            1067                            155
## 11 63.35403 31.11236   0              87                             89
## 12 82.75403 29.58736   0             974                           -131
## 13 60.96236 37.91236   0             133                             33
## 14 58.13736 26.99569   0             216                            120
## 15 80.59569 29.72069   0            1957                             58
## 16 66.65403 29.48736   0             170                             31
## 17 65.74569 28.28736   0             152                             93
## 18 63.12069 37.47069   0             176                             38
##    CHELSA_bio10_18 CHELSA_bio10_10
## 53              92              91
## 54              68             117
## 55              77              74
## 56              64             103
## 57             480             192
## 58             346             236
## 59             237             299
## 60             881             211
## 61             877             233
## 62             461             297
## 63             768             243
## 64             102             307
## 65             530             297
## 66              91             330
## 67              83             333
## 68              33             302
## 69              35             292
## 70              18             309
## 71             134             305
## 1               75             337
## 2               99             328
## 3               64             349
## 4               79             341
## 5               16             161
## 6               29             268
## 7               78             339
## 8               71              63
## 9               14             318
## 10             202             320
## 11               0             354
## 12             665              73
## 13               1             313
## 14               5             323
## 15            1363             225
## 16              57             256
## 17              57             312
## 18               1             311